package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    //要在springmvc.xml中手动导入spring-redis.xml
    //    <import resource="spring-redis.xml"/>
    private JedisPool jedisPool;

    //图片上传 imgFile就是上传的图片文件
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            //获取原始文件名称
            String originalFilename = imgFile.getOriginalFilename();//asdz.jpg

            //获取最后一个 . 的索引值
            int lastIndexOf = originalFilename.lastIndexOf(".");

            //获取文件的后缀名
            String suffix = originalFilename.substring(lastIndexOf - 1);

            //使用UUID重新生成文件名,防止重名
            String fileName = UUID.randomUUID().toString() + suffix;

            //执行文件上传到七牛云
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);

            //将上传图片名称存入Redis小集合，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);

            //上传成功
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);


        } catch (IOException e) {
            e.printStackTrace();
            //图片上传失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //添加功能
    @RequestMapping("/addsetmeal")
    public Result addSetMeal(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        try {
            setmealService.addSetMeal(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return setmealService.pageQuery(queryPageBean);
    }

    //删除功能
    @RequestMapping("/deleteSetmeal")
    public Result deleteSetmeal(Integer setmealId) {
        try {
            setmealService.deleteSetmealyId(setmealId);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId) {

        try {
            List<Integer> checkgroupIds = setmealService.findCheckGroupIdsBySetmealId(setmealId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);

        }
    }

    //更新检查组,并添加关联检查项
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.edit(setmeal, checkgroupIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);

    }
}
